package com.example.experiment4

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity

class PersonInformationActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_person_information)

        // 接收MainActivity传递的信息
        val name = intent.getStringExtra("name") ?: ""
        val gender = intent.getStringExtra("gender") ?: ""
        val studentId = intent.getStringExtra("studentId") ?: ""
        val phone = intent.getStringExtra("phone") ?: ""
        val email = intent.getStringExtra("email") ?: ""
        val hobby = intent.getStringExtra("hobby") ?: ""

        // 填充控件
        findViewById<EditText>(R.id.et_name).setText(name)
        findViewById<EditText>(R.id.et_student_id).setText(studentId)
        findViewById<EditText>(R.id.et_phone).setText(phone)
        findViewById<EditText>(R.id.et_email).setText(email)

        if (gender == getString(R.string.radio_male)) {
            findViewById<RadioButton>(R.id.rb_male).isChecked = true
        } else if (gender == getString(R.string.radio_female)) {
            findViewById<RadioButton>(R.id.rb_female).isChecked = true
        }

        val hobbies = hobby.split(", ")
        findViewById<CheckBox>(R.id.cb_travel).isChecked = hobbies.contains(getString(R.string.checkbox_travel))
        findViewById<CheckBox>(R.id.cb_read).isChecked = hobbies.contains(getString(R.string.checkbox_read))
        findViewById<CheckBox>(R.id.cb_game).isChecked = hobbies.contains(getString(R.string.checkbox_game))
        findViewById<CheckBox>(R.id.cb_shopping).isChecked = hobbies.contains(getString(R.string.checkbox_shopping))

        // 保存按钮
        findViewById<Button>(R.id.btn_save).setOnClickListener {
            val newName = findViewById<EditText>(R.id.et_name).text.toString()
            val newStudentId = findViewById<EditText>(R.id.et_student_id).text.toString()
            val newPhone = findViewById<EditText>(R.id.et_phone).text.toString()
            val newEmail = findViewById<EditText>(R.id.et_email).text.toString()

            val newGender = if (findViewById<RadioButton>(R.id.rb_male).isChecked) {
                getString(R.string.radio_male)
            } else {
                getString(R.string.radio_female)
            }

            val newHobby = mutableListOf<String>()
            if (findViewById<CheckBox>(R.id.cb_travel).isChecked) newHobby.add(getString(R.string.checkbox_travel))
            if (findViewById<CheckBox>(R.id.cb_read).isChecked) newHobby.add(getString(R.string.checkbox_read))
            if (findViewById<CheckBox>(R.id.cb_game).isChecked) newHobby.add(getString(R.string.checkbox_game))
            if (findViewById<CheckBox>(R.id.cb_shopping).isChecked) newHobby.add(getString(R.string.checkbox_shopping))
            val hobbyStr = newHobby.joinToString(", ")

            val intent = Intent()
            intent.putExtra("name", newName)
            intent.putExtra("gender", newGender)
            intent.putExtra("studentId", newStudentId)
            intent.putExtra("phone", newPhone)
            intent.putExtra("email", newEmail)
            intent.putExtra("hobby", hobbyStr)
            setResult(RESULT_OK, intent)
            finish()
        }

        // 取消按钮
        findViewById<Button>(R.id.btn_cancel).setOnClickListener {
            finish()
        }
    }
}